package com.fc.common.util;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * <p>Title: JacksonUtil</p>
 * <p>Description: </p>
 * <p>Company: </p> 
 * @author wanghw
 * @date 2016年3月20日 
 */
public class JacksonUtil {
	
	public static ObjectMapper getMapper(){
		ObjectMapper mapper = new ObjectMapper();
		return mapper;
	}

	/** 
	 * JSON转Object
	 * @param json
	 * @param cls
	 * @return
	 * @throws Exception
	 */
	public static <T> T readValue(String json, Class<T> cls) throws Exception{
		return getMapper().readValue(json, cls);
	}
	
	
	/**
	 * Object转JSON
	 * @param o
	 * @return
	 */
	public static String writeValue(Object o){   
		try {
			return getMapper().writeValueAsString(o);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
	       
	     return null;
	 }   
}
